package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.model.ShopActiveDetail;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ShopActiveDetailView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/24 0024.
 */

public class ShopActiveDetailPerenter extends BasePresent<ShopActiveDetailView> {

    RetrofitSerives retrofitSerives;
    Call<ShopActiveDetail> detailCall;
    Call<ResultMessage4> qrCodeCall;

    public void getShopActiveDetail(final int id) {

        //articlePromotionUsers/selectArticlePromotion/21
        view.showProgress();

        String url = UrlTools.SellerAddressTest + "articlePromotionUsers/selectArticlePromotion/";

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(url);

        detailCall = retrofitSerives.getShopActiveDetail(id);

        detailCall.enqueue(new Callback<ShopActiveDetail>() {
            @Override
            public void onResponse(Call<ShopActiveDetail> call, Response<ShopActiveDetail> response) {

                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setShopActiveDetail(response.body());
                        view.hideProgress();
                    } else {
                        view.showErrorLayout(null);
                        view.toast(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.showErrorLayout(null);
                    view.toast(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ShopActiveDetail> call, Throwable t) {
                view.showErrorLayout(null);
                view.toast(Constants.REQUEST_ERROR_INFO);
            }
        });

    }

    public void getQrCode(String id, int factoryId, int type) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getParams(id, factoryId, type));

        qrCodeCall = retrofitSerives.getQrcode(body);

        qrCodeCall.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {

                        view.setQrCode(response.body());
                        view.hideProgress();
                    } else {
                        view.showErrorLayout(null);
                        view.toast(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.showErrorLayout(null);
                    view.toast(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.showErrorLayout(null);
                view.toast(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    private String getParams(String id, int factoryId, int type) {
        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("id", id);
            paramsJson.put("user_id", factoryId);
            paramsJson.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }

    @Override
    public void stopRequest() {

    }
}
